import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class TaskManager{
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader in = null;
		
		try{
			if (args.length == 2){
					in = new BufferedReader(new FileReader(args[0]));
					
					int countOfThreads =  Integer.parseInt(args[1]);
					
					String string = in.readLine();
					if (string == null){
						throw new NullPointerException(args[0]+"  File is empty ");
						
					} else {
						
						@SuppressWarnings("unchecked")
						ArrayList<String>[] data = new ArrayList[countOfThreads];
				        for (int i = 0; i < countOfThreads; i++)
				        {
				            data[i] = new ArrayList<String>();
				        }
				        
				        int k = 0;
						while (string != null){
							
							data[k].add(string);
							
							k++;
							if (k == countOfThreads){
								k = 0;
							}
							
							string = in.readLine();
						}
						
						for (int i = 0; i < countOfThreads; i++){
							new Worker(data[i]).start();
						}
					}
					
			} else {
				System.out.println("Wrong usage. Wrong arguments in run configurations. Correct usage: \"filename countOfThreads\" ");
				return;
			}
			
		} catch (IOException e){
			System.out.println(e.toString());
			
		} catch (NumberFormatException e){
			System.out.print(e.toString()+". ");
			System.out.println("Please, enter count of threads as positive integer in run configurations. ");
			
		} catch (NullPointerException e){
			System.out.print(e.toString()+". ");
			
		} finally {
			try{
				if (in != null) in.close();
			} catch (IOException e) {
				System.out.println(e.toString());
			}
		}
	}
}